import pygame
from pygame.locals import *
import random
import time

class Base(object):
    def __init__(self, screen, x, y, image):
        self.screen = screen
        self.x = x
        self.y = y
        self.image = pygame.image.load(image)
    #显示
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))


#熊猫类
class Panda(Base):
    #计数器
    bo_num = 0
    def __init__(self, screen):
        Base.__init__(self, screen, 320, 380, "./resources/xm.png")
    #熊猫移动
    def move_right(self):
        #小于右边界
        if self.x <= 580:
            self.x += 50
        else:
            self.x += 0
    def move_left(self):
        #小于左边界
        if self.x < 10:
            self.x -= 0
        else:
            self.x -= 50
    def move_up(self):
        if self.y < 10:
            self.y -= 0
        else:
            self.y -= 50
    def move_down(self):
        if self.y >= 350:
            self.y += 0
        else:
            self.y += 50
#竹子类
class Bamboo(object):
    def __init__(self, screen):
        self.x = 0
        self.y = 0
        self.screen = screen
        self.direction = "right"
        #竹子列表
        self.bamboo_list = []
    # 竹子显示
    def display(self):
        self.move()
        for bo in self.bamboo_list:
            #竹子列表显示
            bo.display()
            #竹子列表类移动方法
            bo.movebamboo()
    #将竹子添加到列表
    def attend(self):
        num = random.randint(1, 300)
        if num == 2 or num == 8:
            self.bamboo_list.append(Bolist(self.x, self.y, self.screen))
    #竹子移动
    def move(self):
        if self.direction == "right":
            self.x += 2
        elif self.direction == "left":
            self.x -= 2
        if self.x > 700:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"
#竹子列表类
class Bolist(Base):
    def __init__(self, x, y, screen):
        Base.__init__(self, screen, x, y, "./resources/zz.png")
    #竹子随机移动
    def movebamboo(self):
        self.y += 0.5

def key_control(play_hero):
    # 获取事件，比如按键等
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                play_hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                play_hero.move_right()
            elif event.key == K_w or event.key == K_UP:
                print('up')
                play_hero.move_up()
            elif event.key == K_s or event.key == K_DOWN:
                print('down')
                play_hero.move_down()
            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                print('space')
def main():
    #初始化游戏
    pygame.init()
    #创建一个窗口显示内容
    screen = pygame.display.set_mode((730, 500))
    #添加背景图片
    background = pygame.image.load("./resources/bj.png")
    #更新内容
    pygame.display.update()
    #实例化panda， bo对象,将screen作为参数传入
    panda = Panda(screen)
    bo = Bamboo(screen)
    while True:
        # 把背景图片放到窗口显示
        screen.blit(background, (0, 0))
        #定义字体大小
        font = pygame.font.SysFont("arial", 50)
        #添加字体内容，计数器
        fine = font.render(str(Panda.bo_num), True, (52, 0, 255))
        #将内容放到窗口显示
        screen.blit(fine, (640, 6))
        #熊猫显示
        panda.display()
        #将竹子添加到列表
        bo.attend()
        #竹子显示
        bo.display()
        #判断熊猫吃竹子
        for boos in bo.bamboo_list:
            #判断竹子和熊猫的位置，如果竹子掉落在熊猫上
            if panda.x <= boos.x <= panda.x + 100 and boos.y >= panda.y - 20:
                #让竹子消失
                bo.bamboo_list.remove(boos)
                #计数器+1，表示熊猫吃到一个竹子
                Panda.bo_num += 1
            #如果竹子在其它地方并且竹子高度紧靠地面
            elif boos.y >= 490:
                #显示游戏结束
                over = font.render("Game Over!", False, (0, 0, 255))
                bo.screen.blit(over, (250, 200))
                #更新
                pygame.display.update()
                time.sleep(2)
                pygame.quit()
                exit()
        pygame.display.update()
        key_control(panda)
if __name__ == "__main__":
    main()